<template>
	<!-- #ifdef APP-PLUS -->
	<view></view>
	<!-- #endif -->

	<!-- #ifdef H5 -->
	<web-view :src="game.url" :webview-styles="{progress: true}"></web-view>
	<!-- #endif -->
</template>

<script>
	var wv; //计划创建的webview
	import downloadClass from "@/common/helper-download.js";

	export default {
		data() {
			return {
				game: {},
			}
		},
		computed: {
			downloadRule() {
				return this.$store.state.app.downloadRule;
			},
			urls() {
				return this.$store.state.download.urls;
			},
		},
		watch: {
			downloadRule() {
				console.log("检测更新");
			}
		},
		methods: {
			gotoDownload(url) {
				console.log("gotoDownload", url)
				// 不存在下载任务中
				if (!this.urls.includes(url)) {
					let download = new downloadClass(this.game, url, true, true, 0, 0, 0, 0);
					console.log("gotoDownload download", download, download.isSuccess)
					this.$store.commit("download/LISTS_ADD", download);
				}
				// console.log("ddddddddd", this.$store.state.download.lists);
				// TODO 跳转到下载列表
				uni.navigateTo({
					url: "download",
					success(res) {
						console.log("success", res)
					},
					fail(res) {
						console.log("failed", res)
					}
				});
			},
			overrideDownload(event) {
				// this.$store.commit("download/CLEAR");
				console.log("overrideDownload", event);
				
				// 安卓的确认在左边, 对调一下
				// #ifdef APP-ANDROID
				uni.showModal({
					title: '提示',
					content: '下载并安装《'+ this.game.name +'》',
					cancelText: "确认",
					cancelColor: "#007aff",
					confirmText: "取消",
					confirmColor: "#000000",
					success: (res) => {
						// 对调之后取消才是确认
						if (res.confirm) {
							
						} else if (res.cancel) {
							this.gotoDownload(event.url);
						}
					}
				});
				// #endif
				
				// #ifndef APP-ANDROID
				uni.showModal({
					title: '提示',
					content: '下载并安装《'+ this.game.name +'》',
					success: (res) => {
						if (res.confirm) {
							this.gotoDownload(event.url);
						} else if (res.cancel) {
				
						}
					}
				});
				// #endif
			}
		},
		onLoad() {
			let that = this;
			let game = uni.getStorageSync("curGame");
			console.log("game", typeof game, game)
			this.game = game;

			// #ifdef APP-PLUS
			console.log("uni.getSystemInfoSync()", uni.getSystemInfoSync());
			wv = plus.webview.create("", "custom-webview", {
				plusrequire: "none",
				'uni-app': 'none',
				top: uni.getSystemInfoSync().statusBarHeight + 44,
				height: uni.getSystemInfoSync().screenHeight - uni.getSystemInfoSync().statusBarHeight - 44,
			});
			// 获取webview标题
			wv.addEventListener("titleUpdate", function(e) {
				uni.setNavigationBarTitle({
					title: e.title,
				})
			})
			wv.onloaded = function(e){
				console.log('Loaded: '+e.target.getURL());
			};
			// wv.onclose = function(e) {
			// 	console.log('onclose: '+e.target.getURL());
			// 	wv.removeEventListener();
			// }
			console.log("this.downloadRule", this.downloadRule)
			// URL拦截
			wv.overrideUrlLoading({
				// effect: "touchstart",
				mode: "reject",
				match: this.downloadRule,
			}, this.overrideDownload);
			
			wv.loadURL(game.url);
			var currentWebview = this.$scope.$getAppWebview();
			currentWebview.append(wv);
			// #endif
		},
		created() {
		// 	// let download = new downloadClass("http://oss-plane-web-dev.cenmaish.com/apk/output/thunder-plane-100.apk", true, false, 0, 0, 0, 0);
		// 	// this.$store.commit("download/DOWNLOAD_ADD", download);
		// 	// this.$store.commit("download/CLEAR");
			console.log("curGame", this.game)
		},
	}
</script>

<style>
</style>